package yz;

/**
 * [剑指 Offer 61. 扑克牌中的顺子](https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/)
 *
 * @Author yangz
 * @Date 2022/9/18 20:49
 */
public class LeetCode61_PukepaiShunZi {
    public boolean isStraight(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                int temp;
                if (nums[i] > nums[j]) {
                    temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
        }
        int min = nums[0];
        int max = nums[4];
        int count = 0;
        for (int num : nums) {
            if (num == 0) {
                count++;
            }
        }
        if (count == 0 && (max - min) != 4) {
            return false;
        }
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] != 0 && nums[i] == nums[i + 1]) {
                return false;
            }
        }
        if (count != 0) {
            int sum = 0;
            for (int j = count + 1; j < nums.length; j++) {
                int i = nums[j] - nums[j - 1] - 1;
                sum += i;
            }
            return sum <= count;
        }
        return true;
    }
}
